package com.lm.orika.example;

import com.lm.orika.dto.UserDTO;
import com.lm.orika.entry.UserEntity;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import java.util.ArrayList;
import java.util.List;

public class OrikaCollectionExample {
    public static void main(String[] args) {
        // 创建一个 List<UserDTO>
        List<UserDTO> userDTOList = new ArrayList<>();
        UserDTO user1 = new UserDTO();
        user1.setName("Alice");
        user1.setAge(25);
        userDTOList.add(user1);

        UserDTO user2 = new UserDTO();
        user2.setName("Bob");
        user2.setAge(28);
        userDTOList.add(user2);

        // 创建 MapperFactory 并映射集合
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(UserDTO.class, UserEntity.class)
                .field("name", "fullName")
                .byDefault()
                .register();

        // 获取 MapperFacade
        MapperFacade mapperFacade = mapperFactory.getMapperFacade();
        List<UserEntity> userEntityList = mapperFacade.mapAsList(userDTOList, UserEntity.class);

        // 打印转换结果
        userEntityList.forEach(userEntity -> {
            System.out.println("Full Name: " + userEntity.getFullName());
            System.out.println("Age: " + userEntity.getAge());
        });
    }
}
